﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Dycox
{
    public interface IBitsComputingSupport
    {
        int Data { get; set; }
    }

    public static class BitsComputingSupport
    {
        public static bool MatchBits(this IBitsComputingSupport obj, int mask)
        {
            return TestBits(obj, mask) == mask;
        }

        public static int TestBits(this IBitsComputingSupport obj, int mask)
        {
            if (obj == null)
                throw new ArgumentNullException("obj");
            return (obj.Data & mask);
        }

        public static int SetBits(this IBitsComputingSupport obj, int mask, bool value)
        {
            if (obj == null)
                throw new ArgumentNullException("obj");

            int data = obj.Data;

            if (value) data |= mask;
            else data &= ~mask;

            obj.Data = data;

            return data;
        }
    }
}
